package com.example.word.demo;

import com.example.word.enums.FontSizeEnum;
import com.spire.doc.Document;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextSelection;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author yuwen
 * @date 2023/4/22
 */
public class GetTitle {
    private static Stack<Integer> stack;
    public static void main(String[] args)throws IOException {
        stack = new Stack<>();
        //加载Word测试文档
        Document doc = new Document();
        doc.loadFromFile("C:\\Users\\yuwen\\Desktop\\设计部分.docx");

        //保存标题内容到.txt文档
        File file = new File("C:\\Users\\yuwen\\Desktop\\GetTitle.txt");
        if (file.exists())
        {
            file.delete();
        }
        file.createNewFile();
        FileWriter fw = new FileWriter(file, true);
        BufferedWriter bw = new BufferedWriter(fw);
        List<Integer> list = new ArrayList<>();
        //遍历section
        for (int i = 0; i < doc.getSections().getCount(); i++)
        {
            Section section = doc.getSections().get(i);
            //遍历Paragraph
            for (int j = 0; j < section.getParagraphs().getCount(); j++)
            {
                Paragraph paragraph = section.getParagraphs().get(j);

                if(paragraph.getText().equals(" ") || paragraph.getText() == null || paragraph.getText().isEmpty())
                    continue;
                //获取标题
                TextSelection textSelection = doc.findString(paragraph.getText(), false, true);
                String fontNameFarEast = textSelection.getAsOneRange().getCharacterFormat().getFontNameFarEast();
                System.out.println(paragraph.getText() + paragraph.getFormat().getOutlineLevel().getValue() + fontNameFarEast);
//                if ( paragraph.getFormat().getOutlineLevel().getValue() == 0 )//段落为“标题1”的内容
//                {
//                    //获取段落标题内容
//                    String text = paragraph.getText();
//                    list.add(0);
//                    float fontSize = textSelection.getAsOneRange().getCharacterFormat().getFontSize();
//                    String s = pbvilging(0);
//                    System.out.println(paragraph.getFormat().getHorizontalAlignment().getValue());
//                    //写入文本到txt文档
//                    bw.write("标题1: "+ text + FontSizeEnum.getFontName(fontSize) +"\r");
//                }
//                //获取标题
//                if ( paragraph.getFormat().getOutlineLevel().getValue() == 1)//段落为“标题2”的内容
//                {
//                    //获取段落标题内容
//                    String text = paragraph.getText();
//                    list.add(1);
//                    float fontSize = textSelection.getAsOneRange().getCharacterFormat().getFontSize();
//                    String s = pbvilging(1);
//                    //写入文本到txt文档
//                    bw.write("标题2: " + text+FontSizeEnum.getFontName(fontSize) + "\r");
//                }
//                //获取标题
//                if ( paragraph.getFormat().getOutlineLevel().getValue() == 2 )//段落为“标题3”的内容
//                {
//                    //获取段落标题内容
//                    String text = paragraph.getText();
//                    list.add(2);
//                    float fontSize = textSelection.getAsOneRange().getCharacterFormat().getFontSize();
//                    String s = pbvilging(2);
//                    //写入文本到txt文档
//                    bw.write("标题3: " + text + FontSizeEnum.getFontName(fontSize) +"\r");
//                }
//                //获取标题
//                if ( paragraph.getFormat().getOutlineLevel().getValue() == 3 )//段落为“标题4”的内容
//                {
//                    //获取段落标题内容
//                    String text = paragraph.getText();
//                    String s = pbvilging(3);
//                    list.add(3);
//                    //写入文本到txt文档
//                    bw.write("标题4: " + text + s+"\r");
//                }


            }
            bw.write("\n");
        }
        bw.flush();
        bw.close();
        fw.close();
    }
    public static String pbvilging(Integer val){
        if(stack.isEmpty()){
            stack.push(val);
        }
        if(val == stack.peek() || val == stack.peek() + 1){
            stack.push(val);
        }else if(val == stack.peek() - 1 || val == stack.peek() - 2){
            int len = stack.size();
            while(len-- > 0){
                stack.pop();
            }
            stack.push(val);
        } else{

            int len = stack.size();
            while(len-- > 0){
                stack.pop();
            }
            stack.push(val);
            return "错误";
        }
        return "";
    }
}